package com.huiyan.hysys.third;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;

public class AliSmsUtil {

    private static final String accessKeyId="LTAI5t8QA19CRR2V15oHyHW8";//
    private static final String accessKeySecret="OqUKmRFUnAYm0frzUc43uKnk5BNoaP";//
    public static final String TEM_ADD_CODE="SMS_114390520";
    public static final String TEM_LOGIN_CODE="SMS_115250125";
    public static final String TEM_FIND_CODE="SMS_204975067";

    /**
     * 发送短信验证码，注册时使用
     * @param phone   手机号
     * @param code  验证码
     * @return
     */
    public static boolean sendRegisterCode(String phone,int code){
        System.err.println("短信验证码：" + code);
        return true;
    }

    /**
     * 发送短信验证码，登陆时使用
     * @param phone
     * @param code
     * @return
     */
    public static boolean sendLoginCode(String phone,int code){
        System.err.println("短信验证码：" + code);
        return true;
    }


    /**
     * 发送短信验证码，找回密码时使用
     * @param phone
     * @param code
     * @return
     */
    public static boolean sendFindPassCode(String phone,int code){
        boolean b = sendSms(phone, TEM_FIND_CODE, code + "");
        return b;
    }


    /**
     * 发送短息验证码
     * @param phone
     * @param temcode
     * @param msg
     * @return
     */
    private static boolean sendSms(String phone,String temcode,String msg){

        //创建配置对象
        Config config = new Config().setAccessKeyId(accessKeyId).setAccessKeySecret(accessKeySecret);

        //访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        Client client;

        try {
            //实例化客服端对象
            client = new Client(config);
            //创建请求对象，并设置信息
            SendSmsRequest sendSmsRequest = new SendSmsRequest().setPhoneNumbers(phone).setSignName("来自邢朋辉的短信").setTemplateCode(temcode).setTemplateCode("{\"code\":\""+msg+"\"}");
            //发送短信验证码
            SendSmsResponse response = client.sendSms(sendSmsRequest);
            System.err.println(response.getBody().message);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

}
